package com.example.campus_activity_backend.controller;

import com.example.campus_activity_backend.common.BaseResponse;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.common.ResultUtil;
import com.example.campus_activity_backend.domain.Class;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.ClassService;
import com.example.campus_activity_backend.service.TeachingStudentInClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/class")
@RestController
public class ClassController {
    @Autowired
    private ClassService classService;

    @Autowired
    private TeachingStudentInClassService teachingStudentInClassService;

    @PostMapping("/add")
    public BaseResponse<Long> addClass(@RequestBody Class classes){
        if(classes == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = classService.addClass(classes);
        return ResultUtil.success(id);
    }

    @PutMapping("/addteacher")
    public BaseResponse<Boolean> addTeacherForClass(@RequestParam("teacher_id") Integer teacherId,
                                                    @RequestParam("id") Integer classId){
        if(teacherId == null || classId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = classService.addTeacherForClass(teacherId, classId);
        return ResultUtil.success(result);
    }

    public BaseResponse<Boolean> addStudenToClass(@RequestParam("id") List<Integer> idList,
                                                  @RequestParam("class_id") Integer classId){
        if(idList == null || classId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teachingStudentInClassService.addStudenToClass(idList, classId);
        return ResultUtil.success(result);
    }
}
